package com.roy.wakeupres.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.roy.wakeupres.entity.ButterflyModel;
import com.roy.wakeupres.entity.ButterflyTask;
import com.roy.wakeupres.mapper.ButterflyModelMapper;
import com.roy.wakeupres.mapper.ButterflyTaskMapper;
import com.roy.wakeupres.query.ButtonQuery;
import com.roy.wakeupres.service.IButterflyTaskService;
import com.roy.wakeupres.utils.Json2Object;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Roy
 * @since 2024-07-09
 */
@Service
public class ButterflyTaskServiceImpl extends ServiceImpl<ButterflyTaskMapper, ButterflyTask> implements IButterflyTaskService {


    @Autowired
    private ButterflyModelMapper butterflyModelMapper;


    @Override
    public void button(ButtonQuery query) {

        // 获取当前日期的起始时间
        LocalDateTime startDate = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime now = LocalDateTime.now();
        ButterflyTask butterflyTask = this.getById(query.getId());
        if(butterflyTask.getTaskStatus() == 1){

            //点击后暂停
            butterflyTask.setTaskStatus(2);
            butterflyTask.setTotal(butterflyTask.getRemaining());
            this.updateById(butterflyTask);
            //根据优先级开启下一条任务
            LambdaQueryWrapper<ButterflyTask> qw1 = new LambdaQueryWrapper<>();
            qw1.ne(ButterflyTask::getId,query.getId());
            qw1.ne(ButterflyTask::getTaskStatus,3);
            qw1.ge(ButterflyTask::getCreateTime , startDate);
            qw1.orderByDesc(ButterflyTask::getPriority);
            List<ButterflyTask> list = this.list(qw1);
            if(Objects.nonNull(list)&&list.size()>0){
                ButterflyTask butterflyTask1 = list.get(0);
                butterflyTask1.setTaskStatus(1);
                butterflyTask1.setButtonTime(now);
                this.updateById(butterflyTask1);
            }

        }else if(butterflyTask.getTaskStatus() == 0||butterflyTask.getTaskStatus() == 2){

            butterflyTask.setButtonTime(now);
            LambdaQueryWrapper<ButterflyTask> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(ButterflyTask::getTaskStatus,1);
            qw2.ge(ButterflyTask::getCreateTime , startDate);
            ButterflyTask one = this.getOne(qw2);
            if(Objects.nonNull(one)){
                one.setTaskStatus(2);
                one.setTotal(one.getRemaining());
                this.updateById(one);
            }
            butterflyTask.setTaskStatus(1);
            this.updateById(butterflyTask);

        }
    }


    public void countDown(){

        // 获取当前日期的起始时间
        LocalDateTime startDate = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
        //1、哪一个钟在计时 1 2 3  状态为1的
        LocalDateTime now = LocalDateTime.now();
        LambdaQueryWrapper<ButterflyTask> qw = new LambdaQueryWrapper<>();
        qw.eq(ButterflyTask::getTaskStatus,1);
        qw.ge(ButterflyTask::getCreateTime , startDate);
        ButterflyTask butterflyTask = this.getOne(qw);
        if(Objects.isNull(butterflyTask)) return;
        // 获取差异的秒数
        long seconds = ChronoUnit.SECONDS.between(butterflyTask.getButtonTime(), now);
//        System.out.println("差异的秒数:"+seconds);
        if((butterflyTask.getTotal() - seconds)>0){
            butterflyTask.setRemaining((int)(butterflyTask.getTotal() - seconds));
//            System.out.println(butterflyTask.getTaskName()+"任务剩余时间："+(int)(butterflyTask.getTotal() - seconds));
            this.updateById(butterflyTask);
        }else {
            //更新已结束任务状态
            butterflyTask.setTaskStatus(3);
            butterflyTask.setRemaining(0);
            butterflyTask.setUpdateTime(now);
            this.updateById(butterflyTask);
            nextCountDown(butterflyTask.getId(),now,startDate);
        }


        //2、随时更新时间
        //3、如果时间为0 ，将为1的状态改为3
    }

    public void nextCountDown(Integer id , LocalDateTime now,LocalDateTime startDate){
        //根据优先级开启下一条任务
        LambdaQueryWrapper<ButterflyTask> qw = new LambdaQueryWrapper<>();
        qw.ne(ButterflyTask::getId,id);
        qw.ne(ButterflyTask::getTaskStatus,3);
        qw.ge(ButterflyTask::getCreateTime , startDate);
        qw.orderByDesc(ButterflyTask::getPriority);
        List<ButterflyTask> list = this.list(qw);
        if(Objects.nonNull(list)&&list.size()>0){
            ButterflyTask butterflyTask1 = list.get(0);
            butterflyTask1.setTaskStatus(1);
            butterflyTask1.setButtonTime(now);
            this.updateById(butterflyTask1);
        }
    }

    @Override
    public void init() {

        List<ButterflyModel> butterflyModels = butterflyModelMapper.selectList(null);
        List<ButterflyTask> tasks = Json2Object.getTaskByDB(butterflyModels);
        this.saveBatch(tasks);
    }

    @Override
    public void start() {

        // 获取当前日期的起始时间
        LocalDateTime startDate = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
        LambdaQueryWrapper<ButterflyTask> qw = new LambdaQueryWrapper<>();
        qw.eq(ButterflyTask::getTaskName,"刷牙");
        qw.ge(ButterflyTask::getCreateTime , startDate);
        System.out.println("美好的一天开始啦！！！");
        ButterflyTask one = this.getOne(qw);
        one.setButtonTime(LocalDateTime.now());
        one.setTaskStatus(1);
//        System.out.println("任务"+one.getTaskName());
        this.updateById(one);
    }
}
